/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include "Creator.h"
#include "Hardware.h"
#include <lib3d/hard/Manager.h>
#include <logs/Log.h>
#include <vector>

namespace liba
{
namespace lib3d
{
namespace hard
{
namespace marmalade
{

static std::vector<Hardware *> * hardwares;
static Creator * creator = 0;

Creator::Creator()
{
	creator = this;
	if( hardwares )
		THROW(Gluck(__FUNCTION__, "Hardwares already exists"));
	
    logs::log() << "Creator for openglES hardware";
        
	hardwares = new std::vector<Hardware *>;

	Hardware * ha = new Hardware();
	hardwares->push_back( ha );
	hard::Manager::add_hardware( ha );
}

hard::Hardware * Creator::get_hardware()const
{
	return hardwares->front();
}

Creator::~Creator()
{
	while( !hardwares->empty() )
	{
		Manager::remove_hardware( hardwares->back() );
		delete hardwares->back();
		hardwares->pop_back();
	}

	logs::log() << "Deleting hardwares\n";
	delete hardwares; hardwares = 0;
}

void * Creator::get_proc_address( const char * proc_name )
{
	return 0 ;
}

void Creator::recreate_hardware()
{
	for(int i = 0; i < hardwares->size(); ++i)
	{
		Hardware * ha = (*hardwares)[i];
		hard::Manager::remove_hardware( ha ); 
		hard::Manager::add_hardware( ha ); 
//		(*hardwares)[i]->invalidate();
	}
}

} // namespace marmalade
} // namespace hard
} // namespace lib3d
} // namespace liba

